﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using UI.Lib;
using Model;

namespace UI.Sysset
{
    /// <summary>
    /// 读取服务器端Session
    /// </summary>
    public partial class FrmSessionManager : BaseNonPageMain
    {
        DataTable dt = null;

        public FrmSessionManager(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
            this.IsCheckRight = false;
        }

        protected override void cbxField_SelectedIndexChanged(object sender, EventArgs e)
        {
            //仅能按照完全匹配查询
            this.cbxOp.Items.Clear(); //清空下框
            cbxOp.Items.Add(new TextValuePair("等于", " "));

            cbxOp.SelectedIndex = 0;
        }

        protected override void AddToolButton()
        {
            this.tspTop.Items.Add(this.CreateToolBtn("R", "刷新列表",
               new EventHandler(delegate
                   {
                       BindGrid(); //重新绑定列表
                   })));

            this.tspTop.Items.Add(this.CreateToolBtn("K", "断开连接",
                   new EventHandler(delegate
                   {
                       DataGridViewRow row = this.GetSelectedRow();
                       if (null == row) return;

                       string selectedKey = Components.Validation.ToString(row.Cells["guid"].Value);
                       if (selectedKey == Context.LoginKey)
                       {
                           Application.Exit(); //结束自己
                           //UI.Lib.Factory.CommonHelper.Entity.LoginOut(Context.LoginKey);
                       }
                       else
                       {
                           UI.Lib.Factory.CommonHelper.Entity.LoginOut(selectedKey);
                           BindGrid();
                       }
                   })));
        }

        protected override bool BeforeBindData()
        {
            if (!this.isClickSearchBtn)
            {
                base.txtInput.Clear(); //重新提取数据时，清空输入框
            }
            this.dgvMain.DataSource = null;
            return true;
        }

        protected override Model.ReturnPageData SetDataSource()
        {
            //加载服务器的Session列表
            Model.ReturnPageData model = new Model.ReturnPageData();
            model.AllRowCount = 1000;

            dt = UI.Lib.Factory.CommonHelper.Entity.GetSessionList(MyPager);

            if (isClickSearchBtn) //点击查询
            {
                if (!string.IsNullOrEmpty(txtInput.Text))
                {
                    TextValuePair p = this.cbxField.SelectedItem as TextValuePair;
                    dt.DefaultView.RowFilter = string.Format("{0}='{1}'", p.OtherName, txtInput.Text);
                }
            }
            
            model.CurrnetPageData = dt;
            return model;
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.IsReadDbSource = false;
        }
    }
}
